package com.tzh.plugin.utils;

import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
import org.mybatis.generator.api.dom.java.JavaVisibility;
import org.mybatis.generator.api.dom.java.Method;
import org.mybatis.generator.api.dom.java.Parameter;

import java.util.List;


public class ServiceUtils {

//	public static Map<String,FullyQualifiedJavaType> classTypes = new HashMap<String,FullyQualifiedJavaType>();

    /**
     * 生成方法
     *
     * @param className
     * @param dtoPkg
     * @param methodName
     * @param
     * @param dtoType    1查询参数 2列表结果集 3.新增 4.删除 5.修改 6-根据Id删除
     * @return
     */
    public static Method generateMethod(String className, String dtoPkg, String methodName, int dtoType) {
        String paramDtoClass = className;
        String paramName = className.toLowerCase();
        FullyQualifiedJavaType returnType = new FullyQualifiedJavaType(className);
        Method method = new Method(methodName);
        switch (dtoType) {
            case 1:
                method.addAnnotation("/**查询多条*/");
                paramDtoClass = dtoPkg + ".query.Query" + className + "Dto";
                returnType = new FullyQualifiedJavaType("Page<" + dtoPkg + ".list.List" + className + "Dto" + ">");
                break;
            case 3:
                method.addAnnotation("/**新增*/");
                paramDtoClass = dtoPkg + ".add.Add" + className + "Dto";
                returnType = new FullyQualifiedJavaType(className);
                break;
            case 4:
                method.addAnnotation("/**根据条件删除*/");
                paramDtoClass = dtoPkg + ".del.Del" + className + "Dto";
                returnType = new FullyQualifiedJavaType(Integer.class.getName());
                break;
            case 5:
                method.addAnnotation("/**修改*/");
                paramDtoClass = dtoPkg + ".modify.Mod" + className + "Dto";
                returnType = new FullyQualifiedJavaType(Integer.class.getName());
                break;

            default:
                paramDtoClass = className;
        }

        method.setReturnType(returnType);
        method.setVisibility(JavaVisibility.PUBLIC);
        FullyQualifiedJavaType paramType = new FullyQualifiedJavaType(paramDtoClass);
        method.addParameter(new Parameter(paramType, paramName));
        method.getBodyLines().clear();
        return method;
    }

    public static Method generateMethod(List<IntrospectedColumn> promarys, String className, String dtoPkg, String methodName, int dtoType) {
        FullyQualifiedJavaType returnType = new FullyQualifiedJavaType(className);
        Method method = new Method(methodName);
        //不需要方法体
        switch (dtoType) {
            case 6:
                //删除
                method.addAnnotation("/**删除*/");
                returnType = new FullyQualifiedJavaType(Integer.class.getName());
                promarys.stream().forEach(col -> {
                    String colName = col.getJavaProperty();
                    FullyQualifiedJavaType type = col.getFullyQualifiedJavaType();
                    method.addParameter(new Parameter(type, colName));
                });
                break;
            case 7:
                method.addAnnotation("/**查询单条*/");
                returnType = new FullyQualifiedJavaType(dtoPkg + ".detail.Detail" + className + "Dto");
                promarys.stream().forEach(col -> {
                    String colName = col.getJavaProperty();
                    FullyQualifiedJavaType type = col.getFullyQualifiedJavaType();
                    method.addParameter(new Parameter(type, colName));
                });
                break;
        }
        method.setReturnType(returnType);
        method.setVisibility(JavaVisibility.PUBLIC);
        return method;
    }
}
